#!/usr/bin env python
# -*- coding: utf-8 -*-
###########################################################################
#   Copyright (C) 2008 by Serj Poltavsky                                  #
#   serge.uliss at gmail.com                                              #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 3 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program. If not, see <http://www.gnu.org/licenses/>   #
###########################################################################

import os
import sys
from dvdfile import DvdFile

class DvdSound(DvdFile):
    def __init__(self, filename, format):
        DvdFile.__init__(self, filename, format)

    def export(self):
        if self.filename is None:
            self.make_silence()

    def get_aformat(self):
        return self.__aformat

    def make_silence(self):
        if os.path.exists(self.tmp_name()):
            if __debug__:
                print >> sys.stderr, "Empty sound already exists. skipping..."
        else:
            from tools.sound import EmptySound
            silence = EmptySound()
            silence.save(self.tmp_name())

        self.filename = self.tmp_name()

    def set_filename(self, filename):
        DvdFile.set_filename(self, filename)
        self.update_aformat()

    def tmp_name(self):
        if self.filename is None:
            return os.path.join(self.tmp_dir(), "silence.ac3")
        else:
            return DvdObject.tmp_name()

    def update_aformat(self):
        self.__aformat = 'unknown'
        if self.filename is None:
            return
        name, ext = os.path.splitext(self.filename)
        if ext.lower() in ('.mp3', '.wav', '.ac3'):
            self.__aformat = ext.lower()[1:]
        elif ext.lower() in ('.mp2', '.m2a'):
            self.__aformat = 'mp2'

    aformat = property(get_aformat)
    filename = property(DvdFile.get_filename, set_filename)